/**  
* 北京智普信科技股份有限公司
* Copyright Right (c) 2012-2015 Beijing Zeepson technology Co.,Ltd. 
* http://www.candowin.com/
* All right reserved. 
*/ 

package com.chenjun.common.exception;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import com.chenjun.common.websocket.WebsocketEndPoint;

/** 
 * @Title: MyException 
 * @Description: TODO
 * @author wayne
 * @date 2016年7月8日 上午10:34:25 
 * @version V1.0   
 */
public class RuntimeExceptionHandle4WebSocket {
	private static final Logger LOGGER = Logger.getLogger(RuntimeExceptionHandle4WebSocket.class);
	/** 
	 * @Fields serialVersionUID : TODO
	 */ 
	
	private static final long serialVersionUID = -3478565343358392161L;
	
	private String disabled;
	
	



	/**
	 * @return disabled 
	 */
	
	public String getDisabled() {
		return disabled;
	}


	/** 
	 * @param disabled 要设置的 disabled 
	 */
	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}


	private WebsocketEndPoint websocket;
	
	
	
	/**
	 * @return websocket 
	 */
	
	public WebsocketEndPoint getWebsocket() {
		return websocket;
	}


	/** 
	 * @param websocket 要设置的 websocket 
	 */
	public void setWebsocket(WebsocketEndPoint websocket) {
		this.websocket = websocket;
	}


	public void printSend(Throwable e){
		System.out.println("this.disabled:"+this.disabled);
		if("0".equals(this.disabled)){
			LOGGER.info("压根不会跑！");
			return;
		}
		TextMessage returnMessage = new TextMessage(getErrorMessage(e));
		try {
			WebSocketSession  session=websocket.getSession();
			if(session!=null){
				session.sendMessage(returnMessage);
			}			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}catch(java.lang.IllegalArgumentException e2){
			LOGGER.info("Websocket connection has closed !");
		}
	}
	
	
	private String getErrorMessage(Throwable e){
		StringBuilder sb = new StringBuilder();
		sb.append("<br/><span style=\"color:red;font-size:14px \">ExceptionName:</span>"+e.getClass().getName()+"<br/>");
		StackTraceElement[] trace = e.getStackTrace();
		if(null!=trace&&trace.length>0){
			for (StackTraceElement traceElement : trace){
				sb.append("<span style=\"color:red;font-size:14px \">FileName:</span>"+traceElement.getFileName()+"<br/>"
						+" <span style=\"color:red;font-size:14px \">ClassName:</span>"+traceElement.getClassName()
						+" <span style=\"color:red;font-size:14px \">LineNumber:</span>"+traceElement.getLineNumber()
						+" <span style=\"color:red;font-size:14px \">MethodName:</span>"+traceElement.getMethodName()+"<br/>");
			}
		}
		
        // Print suppressed exceptions, if any
        for (Throwable se : e.getSuppressed()){
        	sb.append("<span style=\"color:red;font-size:14px \">LocalizedMessage:</span>"+se.getLocalizedMessage()+"<br/>");
        }
        // Print cause, if any
        Throwable ourCause = e.getCause();
        if (ourCause != null){
        	sb.append("<span style=\"color:red;font-size:14px \">\nourCause:</span>"+ourCause.getMessage()+"<br/>");
        }
        return sb.toString();
	}
	
}
